<?php
namespace App\V3\V3_2_7\Controllers;

use App\Rules\JsonArray;
use App\Services\CommonService;
use App\V3\V3_2_7\Models\LogOutStorageModel;
use App\V3\V3_2_7\Services\AllocateService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AllocateController extends BaseController
{
    public function allocate(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'allocate_info' => [
                'required',
                'json',
                new JsonArray([
                    'id' => 'required|integer',//库存id
                    'allocate_num' => 'required|numeric|min:0', //数量
                    'storage_id' => 'required|integer',
                    'shelf_id' => 'integer|min:0',
                    'location_id' => 'integer|min:0',
                    'out_shelf_id' => 'integer|min:0',
                    'out_location_id' => 'integer|min:0',
                    'serial_info' => 'string',
                    'remark' => 'string'
                ])
            ],
        ]);

        //参数错误响应
        if($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $time = time();
        $this->operateLog('调拨',$data, $time);
        $data['allocate_time'] = empty($data['allocate_time'])?$time:strtotime($data['allocate_time']);
        $data['label_id'] = empty($data['label_id'])?'':$data['label_id'];
        $data['label_name'] = empty($data['label_name'])?'':$data['label_name'];
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $data['is_use_shelf'] = $this->isUseShelf();
        $data['allocate_no'] = CommonService::getOrderNo(new LogOutStorageModel,'allocate_no','A');

        $result = AllocateService::allocate($data, $time);
        if($result['code'] === 0){
            CommonService::operateResult($data['user_id'],$time, $result,$result['data']);
            return success();
        }
        return response()->json($result);
    }
    /**
     * 调拨--v3.2.6废弃
     * @param Request $request
     * 2022/9/22 16:19
     * Author: Sun Wu
     * @return \Illuminate\Http\JsonResponse
     */
    public function allocate1(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
//            'id' => 'required|integer',//库存物料id
            'storage_id' => 'required|integer',//调出目标仓库id
//            'allocate_num' => 'required|numeric|min:0',//调出目标仓库id
        ],
            [
                'id.required' => '参数`id`缺失',
                'id.integer' => '参数`id`只能为数字',
//                'allocate_num.required' => '参数`allocate_num`缺失',
//                'allocate_num.integer' => '参数`allocate_num`只能为数字',
//                'allocate_num.min' => '参数`allocate_num`最小只能为0',
            ]);
        //参数错误响应
        if($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $data['batch_title'] = $this->batchTitle();
        $data['is_batch_out'] = $this->getOutWay();
        $data['is_use_shelf'] = $this->isUseShelf();
        $data['allocate_no'] = 'DB'.date('YmdHis').rand(1000,9999);

        if($data['is_use_shelf']) {
            if(empty($data['shelf_id']) || empty($data['location_id'])) {
                return $this->fail('', '已开启货架功能，请选择库位以及货架');
            }
        }

        $this->log('调拨', $data);

        return response()->json(
            AllocateService::allocate($data)
        );
    }
}
